package com.th.supcom.pds.impl.service.internal.phamapply;

import java.util.List;

import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.InpPhamApplyMaster;
import com.th.supcom.pds.domain.vo.InpPhamApplyMasterVO;
import com.th.supcom.pds.impl.common.PdsConstants;
import com.th.supcom.pds.impl.manager.InpPhamApplyMasterManager;
import com.th.supcom.pds.internal.api.phamapply.InpPhamApplyMasterBasicService;

public class InpPhamApplyMasterBasicServiceImpl implements InpPhamApplyMasterBasicService
{
    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (InpPhamApplyMasterBasicServiceImpl.class);

    private InpPhamApplyMasterManager m_inpPhamApplyMasterManager;
    private TranslatorManager m_translatorManager;

    public void setTranslatorManager (TranslatorManager translatorManager)
    {
        m_translatorManager = translatorManager;
    }

    public void setInpPhamApplyMasterManager (InpPhamApplyMasterManager inpPhamApplyMasterManager)
    {
        m_inpPhamApplyMasterManager = inpPhamApplyMasterManager;
    }

    @Override
    public InpPhamApplyMaster getInpPhamApplyMasterByID (Long id)
    {
        s_logger.info ("调用getInpPhamApplyMasterByID参数 ：id:{}", new Object[]
        { id });
        if (null == id)
        {
            s_logger.error ("通过主键查询请领单主表时，主键不能为空");
            throw new IllegalArgumentException ("通过主键查询请领单主表时，主键不能为空");
        }
        try
        {
            InpPhamApplyMaster inpPhamApplyMaster = m_inpPhamApplyMasterManager.getInpPhamApplyMasterByID (id);
            inpPhamApplyMaster = m_translatorManager.translate (inpPhamApplyMaster);
            return inpPhamApplyMaster;
        }
        catch (Exception e)
        {
            s_logger.error ("过主键查询请领单主表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("过主键查询请领单主表异常" + PdsConstants.DEFALUT_HTML_WRAP, e);
        }
    }

    @Override
    public List <InpPhamApplyMaster> findInpPhamApplyMasterListByExample (InpPhamApplyMaster inpPhamApplyMaster)
    {
        s_logger.info ("调用findInpPhamApplyMasterListByExample参数 ：inpPhamApplyMaster:{}", new Object[]
        { inpPhamApplyMaster });
        if (inpPhamApplyMaster == null)
        {
            s_logger.error ("通过条件查询请领单主表时，条件对象不能为空");
            throw new IllegalArgumentException ("通过条件查询请领单主表时，条件对象不能为空");
        }
        try
        {
            List <InpPhamApplyMaster> list = m_inpPhamApplyMasterManager.findInpPhamApplyMasterListByExample (inpPhamApplyMaster);
            list = m_translatorManager.translate (list);
            return list;
        }
        catch (Exception e)
        {
            s_logger.error ("通过条件查询请领单主表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过条件查询请领单主表异常" + PdsConstants.DEFALUT_HTML_WRAP, e);
        }
    }

    @Override
    public int findInpPhamApplyMasterCountByExample (InpPhamApplyMaster inpPhamApplyMaster)
    {
        s_logger.info ("调用findInpPhamApplyMasterCountByExample参数 ：inpPhamApplyMaster:{}", new Object[]
        { inpPhamApplyMaster });
        if (inpPhamApplyMaster == null)
        {
            s_logger.error ("通过条件查询请领单主表记录数时，条件对象不能为空");
            throw new IllegalArgumentException ("通过条件查询请领单主表记录数时，条件对象不能为空");
        }
        try
        {
            return m_inpPhamApplyMasterManager.findInpPhamApplyMasterCountByExample (inpPhamApplyMaster);
        }
        catch (Exception e)
        {
            s_logger.error ("通过条件查询请领单主表记录数异常" + e.getLocalizedMessage (), e);
            throw new IllegalArgumentException ("通过条件查询请领单主表记录数异常" + PdsConstants.DEFALUT_HTML_WRAP, e);
        }
    }

    @Override
    public int modifyInpPhamApplyMaster (InpPhamApplyMaster inpPhamApplyMaster)
    {
        s_logger.info ("调用modifyInpPhamApplyMaster参数 ：inpPhamApplyMaster:{}", new Object[]
        { inpPhamApplyMaster });
        if (inpPhamApplyMaster == null)
        {
            s_logger.error ("更新请领单主表时，条件对象不能为空");
            throw new IllegalArgumentException ("更新请领单主表时，条件对象不能为空");
        }
        try
        {
            return m_inpPhamApplyMasterManager.modifyInpPhamApplyMaster (inpPhamApplyMaster);
        }
        catch (Exception e)
        {
            s_logger.error ("更新请领单主表异常" + e.getLocalizedMessage (), e);
            throw new IllegalArgumentException ("更新请领单主表异常" + PdsConstants.DEFALUT_HTML_WRAP, e);
        }
    }

    @Override
    public int modifyInpPhamApplyMasterByExample (InpPhamApplyMaster inpPhamApplyMaster)
    {
        s_logger.info ("调用modifyInpPhamApplyMasterByExample参数 ：inpPhamApplyMaster:{}", new Object[]
        { inpPhamApplyMaster });
        if (inpPhamApplyMaster == null)
        {
            s_logger.error ("更新请领单主表时，条件对象不能为空");
            throw new IllegalArgumentException ("更新请领单主表时，条件对象不能为空");
        }
        try
        {
            return m_inpPhamApplyMasterManager.modifyInpPhamApplyMasterByExample (inpPhamApplyMaster);
        }
        catch (Exception e)
        {
            s_logger.error ("更新请领单主表异常" + e.getLocalizedMessage (), e);
            throw new IllegalArgumentException ("更新请领单主表异常" + PdsConstants.DEFALUT_HTML_WRAP, e);
        }
    }

    @Override
    public Long addInpPhamApplyMaster (InpPhamApplyMaster inpPhamApplyMaster)
    {
        s_logger.info ("调用addInpPhamApplyMaster参数 ：inpPhamApplyMaster:{}", new Object[]
        { inpPhamApplyMaster });
        if (inpPhamApplyMaster == null)
        {
            s_logger.error ("添加请领单主表时，对象不能为空");
            throw new IllegalArgumentException ("添加请领单主表时，对象不能为空");
        }
        try
        {
            Long seq = m_inpPhamApplyMasterManager.addInpPhamApplyMaster (inpPhamApplyMaster);
            return seq;
        }
        catch (Exception e)
        {
            s_logger.error ("添加请领单主表异常" + e.getLocalizedMessage (), e);
            throw new IllegalArgumentException ("添加请领单主表异常" + PdsConstants.DEFALUT_HTML_WRAP, e);
        }
    }

    @Override
    public int removeInpPhamApplyMasterByID (Long id)
    {
        s_logger.info ("调用addInpPhamApplyMaster参数 ：id:{}", new Object[]
        { id });
        if (id == null)
        {
            s_logger.error ("通过主键删除请领单主表时，主键不能为空");
            throw new IllegalArgumentException ("通过主键删除请领单主表时，主键不能为空");
        }
        try
        {
            return m_inpPhamApplyMasterManager.removeInpPhamApplyMasterByID (id);
        }
        catch (Exception e)
        {
            s_logger.error ("通过主键删除请领单主表异常" + e.getLocalizedMessage (), e);
            throw new IllegalArgumentException ("通过主键删除请领单主表异常" + PdsConstants.DEFALUT_HTML_WRAP, e);
        }
    }

    @Override
    public InpPhamApplyMaster getInpPhamApplyMasterByApplyNo (String applyNo)
    {
        InpPhamApplyMaster applyMaster = new InpPhamApplyMaster ();
        applyMaster.setApplyNo (applyNo);
        List <InpPhamApplyMaster> masterList = this.findInpPhamApplyMasterListByExample (applyMaster);
        if (CollectionUtils.isEmpty (masterList))
        {
            s_logger.error ("未查询到单据号为【" + applyNo + "】的请领单据");
            throw new BusinessException ("未查询到单据号为【" + applyNo + "】的请领单据");
        }
        if (masterList.size () != 1)
        {
            s_logger.error ("查询到多张单据号为【" + applyNo + "】的请领单据，请与计算机中心联系");
            throw new BusinessException ("查询到多张单据号为【" + applyNo + "】的请领单据，请与计算机中心联系");
        }
        return masterList.get (0);
    }

    @Override
    public List <InpPhamApplyMaster> findApplyMasterByNurseCodeAndDispensary (String nurseUnitCode, String dispensary)
    {
        if (StringUtils.isBlank (nurseUnitCode))
        {
            s_logger.error ("通过护理单元和药房查询请领主单据时，护理单元代码不能为空");
            throw new IllegalArgumentException ("通过护理单元和药房查询请领主单据时，护理单元代码不能为空");
        }
        if (StringUtils.isBlank (dispensary))
        {
            s_logger.error ("通过护理单元和药房查询请领主单据时，药房代码不能为空");
            throw new IllegalArgumentException ("通过护理单元和药房查询请领主单据时，药房代码不能为空");
        }
        InpPhamApplyMasterVO applyMasterVO = new InpPhamApplyMasterVO ();
        applyMasterVO.setNurseUnit (nurseUnitCode);
        applyMasterVO.setDispensary (dispensary);
        List <InpPhamApplyMaster> masterList;
        try
        {
            masterList = m_inpPhamApplyMasterManager.findApplyMasterByNurseCodeAndDispensary (applyMasterVO);
            masterList = m_translatorManager.translate (masterList);
        }
        catch (Exception e)
        {
            s_logger.error ("通过护理单元和药房查询请领主单据异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过护理单元和药房查询请领主单据异常", e);
        }
        if (CollectionUtils.isEmpty (masterList))
        {
            s_logger.info ("未查询到护理单元在当前药房未执行的请领单据");
            throw new BusinessException ("未查询到护理单元在当前药房未执行的请领单据");
        }
        return masterList;
    }

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

}
